# Import the base Model class from Flask-AppBuilder, which this model will inherit from.
from flask_appbuilder import Model
# Import various column types from SQLAlchemy to define the database schema.
from sqlalchemy import (
    Boolean,
    Column,
    Integer,
    String,
    Text,
)

# Import the Flask application instance to access its configuration.
from myapp.app import app
# Import a custom base model that may contain shared logic or properties.
from myapp.models.base import MyappModelBase
# Import a custom audit mixin for tracking creation and modification details.
from myapp.models.helpers import AuditMixinNullable

# Get the metadata object from the base Model class. Metadata stores information about the database schema.
metadata = Model.metadata
# Get the application's configuration object.
conf = app.config


# Define the Metadata_metric class, which maps to the 'metadata_metric' table.
# This model represents the metadata for a business or system metric.
class Metadata_metric(Model, AuditMixinNullable, MyappModelBase):
    # Specifies the name of the database table for this model.
    __tablename__ = 'metadata_metric'
    # Defines the primary key column for the table.
    id = Column(Integer, primary_key=True)
    # Defines a column for the application or product this metric belongs to.
    app = Column(String(100), nullable=False)
    # Defines a column for the internal or technical name of the metric.
    name = Column(String(300), nullable=True)
    # Defines a column for the user-friendly display name or label of the metric.
    label = Column(String(300), nullable=True)
    # Defines a column for a detailed description of the metric.
    describe = Column(String(500), nullable=False)
    # Defines a column for the metric's definition or calculation logic ('caliber').
    caliber = Column(Text(65536), nullable=True, default='')
    # Defines a column for the type of the metric (e.g., atomic, derived).
    metric_type = Column(String(100), nullable=True)  # 指标类型  	原子指标  衍生指标
                                                       # Metric type: Atomic metric, Derived metric
    # Defines a column for the importance level of the metric.
    metric_level = Column(
        String(100), nullable=True, default='普通'
    )  # 指标等级   普通  重要  核心
       # Metric level: Normal, Important, Core
    # Defines a column for the time dimension or aggregation period of the metric.
    metric_dim = Column(String(100), nullable=True, default='')  # 指标维度   天  月   周
                                                                  # Metric dimension: Day, Month, Week
    # Defines a column for the data type or category of the metric.
    metric_data_type = Column(String(100), nullable=True, default='')  # 指标类型  营收/规模/商业化
                                                                      # Metric data type: Revenue/Scale/Commercialization
    # Defines a column for the person or team responsible for this metric.
    metric_responsible = Column(String(200), nullable=True, default='')  # 指标负责人
                                                                          # Metric responsible person
    # Defines a column for the lifecycle status of the metric.
    status = Column(String(100), nullable=True, default='')  # 状态  下线  上线   创建中
                                                             # Status: Offline, Online, Creating
    # Defines a column to store IDs of related tasks, likely as a comma-separated string.
    task_id = Column(String(200), nullable=True, default='')  # 所有相关任务id
                                                              # All related task IDs
    # Defines a boolean flag to indicate if the metric is publicly visible.
    public = Column(Boolean, default=True)  # 是否公开
                                             # Is it public?
    # Defines a column for storing extra or expanded information, likely as a JSON string.
    expand = Column(Text(65536), nullable=True, default='{}')

    # Defines the official string representation of a Metadata_metric instance.
    def __repr__(self):
        return self.name

    # This method creates a new, unsaved copy of the current metric instance.
    def clone(self):
        """
        Creates a deep copy of the metric object, useful for templating or duplication.
        The new object is not saved to the database.
        """
        return Metadata_metric(
            app=self.app,
            name=self.name,
            describe=self.describe,
            caliber=self.caliber,
            metric_type=self.metric_type,
            metric_level=self.metric_level,
            metric_dim=self.metric_dim,
            metric_data_type=self.metric_data_type,
            metric_responsible=self.metric_responsible,
            status=self.status,
            task_id=self.task_id,
            expand=self.expand,
        )
